﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MutilLanguage_Search.Connection;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using MutilLanguage_Search.Controller;
using MutilLanguage_Search.Data;
using System.Windows.Forms;
namespace MutilLanguage_Search.Controller
{
    class SourceAnalysis
    {
        public static ListBox Synonym;
        public static ListBox Translate;
        public static void ParseVdictEN(string source, string key)
        {

            string phanloai = "<\\s*div class=\"phanloai\"[^>]*>(.*?)<\\s*/\\s*div>";
            string idioms = "<\\s*div class=\"idioms\"[^>]*>(.*?)<\\s*/\\s*div>";
            string pattern = "<\\s*div class=\"phanloai\"[^>]*>(.*?)<\\s*/\\s*div>|<\\s*ul class=\"list1\"[^>]*>(.*?)<\\s*/\\s*ul>|<\\s*div class=\"idioms\"[^>]*>(.*?)<\\s*/\\s*div>";
            Regex myRegex = new Regex(pattern);
            MatchCollection mc = myRegex.Matches(source);
            bool check = false;
            foreach (Match m in mc)
            {
                myRegex = new Regex(phanloai);
                if (myRegex.Match(m.Value).Success)
                {
                    check = true;
                }
                else
                {
                    myRegex = new Regex(idioms);
                    if (myRegex.Match(m.Value).Success)
                    {
                        check = false;
                    }
                }
                if (check)
                {
                    string pattern2 = "<\\s*b[^>]*>(.*?)<\\s*/\\s*b>";
                    myRegex = new Regex(pattern2);
                    Match mm = myRegex.Match(m.Value);
                    string t = (HtmlTagAnalysis.StripTagsCharArray(mm.Value));
                    t = HtmlTagAnalysis.RemoveParentheses(t);
                    string[] str = HtmlTagAnalysis.SlitString(t);
                    foreach (string s in str)
                    {
                        if (!s.Equals(""))
                        {
                            Modify.setTranslate(s, 1);
                            Translate.Items.Add(s);
                            check = false;
                            break;
                        }
                    }
                }
            }
            //Tu dong nghia
            int i = 1;
            List<String> Synonyms = Office.GetSynonyms(key.Trim());
            Modify.setSynonyms(key, 1);
            Synonym.Items.Add(key);
            if (Synonyms.Count != 0)
            {
                foreach (String syn in Synonyms)
                {
                    if (i <= 2 && !syn.Equals(""))
                    {
                        i++;
                        Modify.setSynonyms(syn, 1);
                        Synonym.Items.Add(syn);
                    }
                }
            }
        }
        public static void ParseGoogeSyn(HtmlAgilityPack.HtmlDocument doc)
        {
            HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//h3[@class=\"r\"]/a");
            foreach (HtmlNode item in links)
            {
                string str = item.Attributes["href"].Value.Replace("/url?q=", string.Empty);
                int i = str.IndexOf(@"&", StringComparison.Ordinal);
                str = str.Substring(0, i);
                try
                {
                    Uri link = new Uri(str);
                    Modify.setLinksSyn(str, "1");
                }
                catch
                {
                    continue;
                }
            }
        }
    }
}
